<template>
  <el-config-provider :locale="locale" :size="size">
    <router-view />
  </el-config-provider>
</template>
<script setup>
import useAppStore from "./store/modules/app";
const appStore = useAppStore();
import { ElConfigProvider } from "element-plus";
import zhCn from "element-plus/dist/locale/zh-cn"; // 中文语言
import en from "element-plus/dist/locale/en"; // 英文语言
import thTw from "element-plus/dist/locale/zh-tw"; //繁体
import defaultSettings from "@/settings";
import { getToken } from "@/utils/auth";
const { proxy } = getCurrentInstance();

if (getToken()) {
  let arr = ["/","/index", "/login", "/register", "/forget", "/edit/pwd"];
  if (arr.includes(window.location.pathname)) {
    window.location.pathname;
    let redirect = proxy.$cookieShare.get("redirect");
    if (redirect) {
      window.location.href = redirect;
      proxy.$cache.cookie.remove("redirect");
    } else {
      window.location.href = import.meta.env.VITE_Page_Index;
    }
  }
}
proxy.$cache.local.remove("lang");
const lang = computed(() => {
  return useAppStore().lang;
});
const locale = ref(zhCn);
let langShare = proxy.$cookieShare.get("langShare");
if (langShare && langShare != lang.value) {
  appStore.setLang(langShare);
  window.location.reload();
}

const size = ref(defaultSettings.defaultSize);

size.value = useAppStore().size;

watch(
  lang,
  (val) => {
    if (val == "en") {
      locale.value = en;
    } else if (val == "zh-tw") {
      locale.value = thTw;
    } else {
      locale.value = zhCn;
    }
  },
  {
    immediate: true,
  }
);
</script>
